package com.nut.lmsc.api.merchant;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.nut.lmsc.common.Result;
import com.nut.lmsc.entity.Category;
import com.nut.lmsc.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/merchant/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    @GetMapping("/list")
    public Result<IPage<Category>> list(@RequestParam(required = false) String name,
                                        @RequestParam(required = false) Integer status,
                                        @RequestParam(required = false, defaultValue = "1") Integer pageNo,
                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
        return categoryService.listPage(name, status, pageNo, pageSize);
    }

    @GetMapping("/getTree")
    public Result<List<Category>> getTree(@RequestParam(required = false, defaultValue = "0") Integer pid) {
        return categoryService.getTree(pid);
    }

    @GetMapping(value = "/{id}")
    public Result<Category> detail(@PathVariable Integer id) {
        return categoryService.getById(id);
    }

    @GetMapping("/listByPid")
    public Result<List<Category>> listByPid(@RequestParam(required = false, defaultValue = "0") Integer pid) {
        return categoryService.listByPid(pid);
    }

    @PostMapping
    public Result add(@RequestBody Category category) {
        return categoryService.add(category);
    }

    @PutMapping
    public Result update(@RequestBody Category category) {
        return categoryService.updateById(category);
    }

    @DeleteMapping
    public Result delete(@RequestBody Set<Integer> ids) {
        return categoryService.deleteByIds(ids);
    }

}